package com.servlet.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

public class Cookie_util {
	
	private HttpServletRequest request=null;
	public Cookie_util( HttpServletRequest req){
		 this.request=req;
	}
	
	public Cookie getCookiebyName(String name){
	   
			   Cookie[] cookies=request.getCookies();
			   
			    if(cookies==null)
			    	System.out.println("none any cookie");
			    
			    else {
			    	try{
			    		if(cookies.length==0)
			    			System.out.println("can't write cookie");
			    		else{
			    			for(int i=0 ;i<cookies.length;i++){
			    				if(cookies[i].getName().equals(name))
			    					return cookies[i];
			    			    				    				
			    			}
			    		}
			    		
			    	}catch( Exception e){
			    		System.out.println(e);
			    	}			    	
			    }
       return null;
		
	}
	 public Cookie setValueByname( String name,String vlaue)
	 { Cookie ck =getCookiebyName(name);
	 if(ck!=null)
	 { ck.setValue(vlaue);
	    return ck;
	 }
	 else return null;
	 }
	
     public String getValueByname( String name)
     {   Cookie ck =getCookiebyName(name);
    	 if(ck!=null)
    	   return ck.getValue();
    	 else
    	   return null;
     }
     public Cookie delCookieByname(String name )
     {
    	 
    	 Cookie ck =getCookiebyName(name);
    	 if(ck!=null)
    	 {
    	 ck.setMaxAge(0);   	 
         ck.setPath("/stusys");
         return ck;
    	 }
    	 else{
    		 System.out.println("del_fail");
    		   return null;
    	 }
    	 
     }
     
     
     
     
     
}
